$(function () {
    //页面加载完成获取第一页数据
    getGradeConfigList(1);
    $('body').on('click','.updateStatus',function () {
        var status= $(this).data("status");
        var integralRuleId=$(this).data("id");
        var obj = $(this);
        if(status==3){
            icModale(2,"删除后不可恢复，确认删除么？",0,function () {
                updateIntegralStatus(integralRuleId,status,obj);
            });
        }else if(status==2){
            icModale(2,"确认冻结停用？",0,function () {
                updateIntegralStatus(integralRuleId,status,obj);
            });
        }
        else if(status==1){
            icModale(2,"确认回复么？",0,function () {
                updateIntegralStatus(integralRuleId,status,obj);
            });
        }
    });
    validateData();
    $('body').on('click','#onBack',function (){
        onBackHistory();
    });
    $('body').on('click','#submitUserIntegralRule',function (){
        saveUserIntegralRule();
    });
    inputRadioSel($(".i-checks"),selectCall);

    //表格中单个字段的修改
    $('body').on('click', '.resetdBtn', function() {
        var  score  =$(this).data('score');
        var  type = $(this).data('type');
        var   id = $(this).data('id');
        parent.layer.open({
            type: 1,
            area: ['380px'],
            skin: 'layui-layer-demo',
            title: '修改此项',
            content: '<div class="m-t m-r m-l m-b"><input class="form-control" value="'+score+'" id="userIntegralScore" name="userIntegralRule.score" type="text"></div>',
            btn: '确定',
            yes: function(index, layero) {
                var userIntegralScore = layero.find('#userIntegralScore').val();
                $.ajax({
                    url:baseloaction+"/admin/user/cus/saveUserIntegralRuleScore",
                    type:"post",
                    data:{"id":id,"score":userIntegralScore},
                    dataType:"json",
                    success:function (result) {
                        parent.layer.close(index);
                        if (result.success == true) {
                            icModale(0,result.message);
                            if(type==1||type==3){
                                $('.userIntegralScore_'+id).html(userIntegralScore+"分数");
                            }
                            if(type==2){
                                $('.userIntegralScore_'+id).html(userIntegralScore+"%");
                            }
                            $('.userIntegralScore_'+id).append('<span class="resetdBtn fs16 c-red" data-id="'+id+'" data-type="'+type+'"  data-score="'+userIntegralScore+'" title="修改"><i class="fa fa-pencil"></i></span>');
                        } else {
                            icModale(0,result.message);
                        }
                    }
                });
            }
        });
    });
});
/**
 * 取消
 */
function onBackHistory() {
    window.history.go(-1);
}
/**
 * 选择回调
 */
function selectCall(em) {
    if(em.name=='type'){
        getProportionIntegral(em.value);
    }
}
/**
 * 控制比例分数 和固定分数
 */
function getProportionIntegral(value) {
    if(value==1){
        $(".userIntegralRuleTermDepict").show();
        $("#userIntegralScore").removeClass();
        $('.integralGive').show();
        $(".percentRemove").hide();
    }else if(value==2){
        $("#userIntegralScore").attr("class", "input-group m-b");
        $(".percentRemove").show();
        $('.integralGive').show();
        $(".userIntegralRuleTermDepict").show();
    }else if(value==3){
        $('.integralGive').hide();
        $(".userIntegralRuleTermDepict").hide();
    }
}

/**
 * 提交方法
 */
function saveUserIntegralRule() {
    var params = $("#userIntegralFrom").serializeJSON();
    params = JSON.stringify(params);
    $.ajax({
        url:baseloaction+"/admin/user/cus/saveUserIntegralRule",
        data:params,
        type:"post",
        dataType:"json",
        contentType:'application/json;charset=utf-8',
        success:function (result) {
            if (result.success == true) {
                icModale(1,result.message,1,function () {
                    window.location.href=baseloaction+"/admin/user/cus/toCusUserIntegralRuleListPage";
                });
            } else {
                icModale(0,result.message);
            }
        }
    });
}
/**
 * 修改状态
 */
function updateIntegralStatus(integralRuleId,status,obj) {
    $.ajax({
        url: baseloaction + "/admin/user/cus/updateUserIntegralRuleStatus",
        type: 'post',
        dataType: 'json',
        data: {"integralRuleId": integralRuleId, "status": status},
        success: function (result) {
            if (result.success == true) {
                icModale(1, result.message, 1, function () {
                    if(status==1){
                        obj.data('status','2');
                        obj.data('original-title','停用');
                        obj.html('<i class="fa fa-unlock-alt"></i>');
                        $('.userIntegralRule_'+integralRuleId).html('<span class="label label-info">正常</span>')
                    }else if(status==2){
                        obj.data('status','1');
                        obj.data('original-title','回复');
                        obj.html('<i class="fa fa-history"></i>');
                        $('.userIntegralRule_'+integralRuleId).html('<span class="label label-danger">停用</span>')
                    }else if(status==3){
                        obj.parents('tr').remove();
                        var totalCountNum =$('#totalCountNum').val();
                        $("#totalCountNum").val((totalCountNum - 1));
                        if((totalCountNum-1)==0){
                            $("#tbodyIntegral").append('' +
                                '<td colspan="6" class="text-center">'
                                +'<div class="noDataTipsBox">'
                                +'<p><span class="c-4e fs16">亲，还没有数据~~~</span></p>'
                                +'</div>'
                                +'</td>'
                            );
                        }
                    }
                });
            } else {
                icModale(0, result.message);
            }
        }
    });
}
/**
 * 获取角色列表页面
 * @param pageNo 当前页码
 */
function getGradeConfigList(pageNo) {
    var params = '{}';
    var obj = {
        url: baseloaction + '/admin/user/cus/queryAjaxUserIntegralRuleList',//请求URL
        contextId:'integral_list_content',//响应内容页面设置内容ID
        params:params,//请求参数
        pageNo:pageNo,//页码
        pageCall:getGradeConfigList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId:'select-all-role', //全选框ID
        callback:function () {//数据加载完成回调

        }
    };
    ajaxGetListData(obj)
}
